package com.itheima.consumer.mq;

import com.itheima.consumer.constant.MqExChange;
import com.itheima.consumer.constant.MqQueue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author czt
 * @version 1.0
 * @since 2024/11/6
 *
 * 如果有多个消费者绑定了同一队列，一条消息只会被一个消费者消费
 * 如果一个交换机绑定了多个队列，那么每个队列都会收到这条消息
 */
@Slf4j
@Component
public class SpringRabbitMqListener {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqQueue.DIRECT_1),
            exchange = @Exchange(name = MqExChange.D_TEST),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String message) {
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqQueue.DIRECT_2),
            exchange = @Exchange(name = MqExChange.D_TEST),
            key = {"red","yellow"}
    ))
    public void listenDirectQueue2(String message) {
        System.out.println("message2 = " + message);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(Map<String, Object> msg) {
        System.out.println("msg = " + msg);
    }
}
